var wx_share = {};
(function(share) {
	share.currentIndex=0;
	share.localIds=[];
	share.showImg=function(imgs,index){
		wx.previewImage({
			current: index, // 当前显示图片的http链接
    		urls: imgs // 需要预览的图片http链接列表
		});
	},
	share.uploadImage = function(callback) {
		var customerId = $.util.getParam("customerId");
		var me=this;
		wx.chooseImage({
			count:1,
      		sizeType:'compressed', // 可以指定是原图还是压缩图，默认二者都有
			success: function(res) {
				var localIds = res.localIds;
				var status = {
					len: localIds.length,
				};
				status.callback = callback;
				me.localIds=localIds;
				share.currentIndex=0;
				if (localIds && localIds.length > 0) {
					setTimeout(function() {
						me.uploadSingle(0,status);
					}, 150);
				} else {
					$.util.alert("请选择一张图片上传");
				}
			}
		});
	},
	share.uploadSingle=function(index,status) {
		var me=this;
		if(me.localIds.length>1) {
			$.util.pageLoading.show("正在上传第"+(me.currentIndex+1)+"张");
		} else {
			$.util.pageLoading.show("上传中");
		}
		wx.uploadImage({
			localId: me.localIds[index], // 需要上传的图片的本地ID，由chooseImage接口获得
			isShowProgressTips: 0, // 默认为1，显示进度提示
			success: function(res) {
				status.index = index;
				share.__upload(res, status,function(){
					if(me.localIds.length>0) {
						if(me.currentIndex>=me.localIds.length-1) {
							$.util.pageLoading.hide();
						}
					} else {
						$.util.pageLoading.hide();
					}
					if(me.currentIndex < me.localIds.length-1) {
						me.uploadSingle(++me.currentIndex,status);
					}
				});
			}
		});
	}
	share.uploadMultImage = function(callback) {
		var customerId = $.util.getParam("customerId");
		var me=this;
		wx.chooseImage({
			success: function(res) {
				var localIds = res.localIds;
				var status = {
					len: localIds.length,
				};
				status.callback = callback;
				share.currentIndex=0;
				me.localIds=localIds;
				if (localIds && localIds.length > 0) {
					setTimeout(function() {
						me.uploadSingle(me.currentIndex,status);
					}, 150);
				} else {
					$.util.alert("请选择一张图片上传");
				}
			}
		});
	}
	share.showOrhideMenu=function(bool){
		if(bool)
		{
			wx.showOptionMenu();
		}
		else
		{
			wx.hideOptionMenu();
		}
			
	}
	share.__upload = function(res, status,uploadCallback) {
		var serverId = res.serverId; // 返回图片的服务器端ID
		var me=this;
		var customerId=$.util.getParam("customerId");
		$.ajax({
			url:"Upload/uploadToOss.json?customerId="+customerId+"d=" + (+new Date()),
			data:{
				"mediaId": serverId
			},
			type:'POST',
			dataType:'JSON',
			success:function(data){
				if (!data.code) {
					uploadCallback && uploadCallback();
					status.callback && status.callback(data, status);
				}else{
					$.util.alert(data.msg);
				}
			},
			error:function(){
				$.util.alert("上传失败");
			}
		})
//		$.req.get("Upload/uploadToOss.json?customerId="+customerId+"d=" + (+new Date()), {
//			"mediaId": serverId
//		}, function(data) {
//			if (!data.code) {
//				uploadCallback && uploadCallback();
//				status.callback && status.callback(data, status);
//			}else{
//				$.util.alert(data.msg);
//			}
//		},function(){
//			$.util.alert("上传失败");
//		});
	}
	share.init = function(config) {
		var me = this;
		var pkg = config.pkg;
		var apiList = [
			'onMenuShareTimeline',
			'onMenuShareAppMessage',
			'onMenuShareQQ',
			'onMenuShareWeibo',
			'chooseImage',
			'uploadImage',
			'previewImage',
			'downloadImage',
			'showAllNonBaseMenuItem',
			'hideOptionMenu',
			'showOptionMenu'
		];
		wx.config({
			debug: false,
			appId: pkg.appId,
			timestamp: pkg.timestamp,
			nonceStr: pkg.nonceStr,
			signature: pkg.signature,
			jsApiList: apiList
		});
		wx.ready(function() {
			if (config.title) {
				wx.showAllNonBaseMenuItem();
				wx.onMenuShareAppMessage({
					desc: config.desc || "", // 分享描述
					title: config.title || "", // 分享标题
					link: config.link || "", // 分享链接
					imgUrl: config.imgUrl || "" // 分享图标
				});
				wx.onMenuShareTimeline({
					desc: config.desc || "", // 分享描述
					title: config.title || "", // 分享标题
					link: config.link || "", // 分享链接
					imgUrl: config.imgUrl || "" // 分享图标
				});
				wx.onMenuShareQQ({
					desc: config.desc || "", // 分享描述
					title: config.title || "", // 分享标题
					link: config.link || "", // 分享链接
					imgUrl: config.imgUrl || "" // 分享图标
				});
				wx.onMenuShareWeibo({
					desc: config.desc || "", // 分享描述
					title: config.title || "", // 分享标题
					link: config.link || "", // 分享链接
					imgUrl: config.imgUrl || "" // 分享图标
				});
				
			}
		});
	}
})(wx_share);